package com.example.cd5160866.urlapp.modelv1;

import android.content.Context;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import com.example.cd5160866.urlapp.models2.ModelRecyclerAdapterV2;

/**
 * Created by cd5160866 on 16/4/28.
 */
public class ModelRecyclerView extends RecyclerView {
    private static final int DEFAULT_GRID_SPAN = 3;
    public static final int MODEL_LIST_VIEW_VERTICAL = 0;
    public static final int MODEL_LIST_VIEW_HORIZONTAL = 1;
    public static final int MODEL_GRID_VIEW_VERTICAL = 2;

    private int currentModel = MODEL_LIST_VIEW_VERTICAL;
    private ModelRecyclerAdapterV2 modelRecyclerAdapter;

    /**
     * 默认gridView行数
     */
    private int gridSpanCount = DEFAULT_GRID_SPAN;

    private LinearLayoutManager mManager;


    public ModelRecyclerView(Context context) {
        super(context);
        init();
    }

    public ModelRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        updateLayoutManager();
        addOnItemTouchListener(new OnRecyclerItemClickListener(this) {
            @Override
            public void onItemClick(RecyclerView.ViewHolder vh) {
                if (clickListener != null) {
                    clickListener.onItemClick(vh);
                }
            }

            @Override
            public void onItemLongClick(ViewHolder vh) {
                if (longClickListener != null) {
                    longClickListener.onItemLongClick(vh);
                }
            }
        });
    }

    private void updateLayoutManager() {
        switch (currentModel) {
            case MODEL_LIST_VIEW_VERTICAL:
                mManager = new LinearLayoutManager(getContext());
                mManager.setOrientation(LinearLayoutManager.VERTICAL);
                break;
            case MODEL_LIST_VIEW_HORIZONTAL:
                mManager = new LinearLayoutManager(getContext());
                mManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                break;
            case MODEL_GRID_VIEW_VERTICAL:
                mManager = getGridLayoutManager();
                mManager.setOrientation(LinearLayoutManager.VERTICAL);
                break;
            default:
                break;
        }
        setLayoutManager(mManager);
    }

    private GridLayoutManager getGridLayoutManager() {
        final GridLayoutManager manager = new GridLayoutManager(getContext(), gridSpanCount);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (modelRecyclerAdapter == null) {
                    return -1;
                } else {
                    if (modelRecyclerAdapter.getItemViewType(position) == ModelRecyclerAdapterV2.TYPE_DEFULT) {
                        return 1;
                    } else {
                        return gridSpanCount;
                    }
                }
            }
        });
        return manager;
    }

    public void setModelAdapter(ModelRecyclerAdapterV2 adapter) {
        setAdapter(adapter);
    }

    @Override
    public void setAdapter(Adapter adapter) {
        if (!(adapter instanceof ModelRecyclerAdapterV2)) {
            throw new IllegalArgumentException(" please use ModelRecyclerAdapter instead");
        }
        this.modelRecyclerAdapter = (ModelRecyclerAdapterV2) adapter;
        super.setAdapter(adapter);
    }

    public void setLayoutModel(int model) {
        this.currentModel = model;
        updateLayoutManager();
    }

    public int getDefultGirdSpanCount() {
        return gridSpanCount;
    }

    public void setDefultGirdSpanCount(int defaultSpanCount) {
        this.gridSpanCount = defaultSpanCount;
        updateLayoutManager();
    }

    private OnItemClickListener clickListener;

    public void setOnItemClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    private OnItemLongClickListener longClickListener;

    public interface OnItemLongClickListener {
        /**
         * 长按监听
         */
        void onItemLongClick(RecyclerView.ViewHolder viewHolder);
    }

    public interface OnItemClickListener {
        void onItemClick(RecyclerView.ViewHolder viewHolder);
    }

    private abstract class OnRecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
        private GestureDetectorCompat mGestureDetector;
        private RecyclerView recyclerView;

        private OnRecyclerItemClickListener(RecyclerView recyclerView) {
            this.recyclerView = recyclerView;
            mGestureDetector = new GestureDetectorCompat(recyclerView.getContext(), new ItemTouchHelperGestureListener());
        }


        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            return mGestureDetector.onTouchEvent(e);
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {
            mGestureDetector.onTouchEvent(e);
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
        }

        /**
         * 这个类用于判定recycleview的滑动状态
         */
        private class ItemTouchHelperGestureListener extends GestureDetector.SimpleOnGestureListener {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null) {
                    RecyclerView.ViewHolder vh = recyclerView.getChildViewHolder(child);
                    onItemClick(vh);
                }
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null) {
                    RecyclerView.ViewHolder vh = recyclerView.getChildViewHolder(child);
                    onItemLongClick(vh);
                }
            }
        }

        public abstract void onItemLongClick(RecyclerView.ViewHolder vh);

        public abstract void onItemClick(RecyclerView.ViewHolder vh);
    }

}
